<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');

function segment_explode($seg)
{
	//세크먼트 앞뒤 '/' 제거후 uri를 배열로 반환
	$len = strlen($seg);
	if(substr($seg, 0, 1) == '/')
	{
		$seg = substr($seg, 1, $len);
	}
	$len = strlen($seg);

	if(substr($seg, -1) == '/')
	{
		$seg = substr($seg, 0, $len-1);
	}
	$seg_exp = explode("/", $seg);

	return $seg_exp;
}

/**
 * url중 키값을 구분하여 값을 가져오도록.
 *
 * @param Array $url : segment_explode 한 url값
 * @param String $key : 가져오려는 값의 key
 * @return String $url[$k] : 리턴값
 */
function url_explode($url, $key)
{
	$cnt = count($url);
	for($i=0; $cnt>$i; $i++ )
	{
		if($url[$i] ==$key)
		{
			$k = $i+1;
			return $url[$k];
		}
	}
}

/**
 * window, linux 개발환경에 따른 경로 변경
 *
 * @param String : 절대경로
 * @return String : 서버경로
 */
function convert_path($path){
	if($path){
		$root = realpath(FCPATH);
		if(strstr($root, ':')) {
			$arr = explode(':', $root);
			$path = $root.str_replace('/', '\\', $path);
		} else {
			$path = $root.$path;
		}
	}	
	return $path;
}

/**
 * 검색파라미터 URL 생성
 *
 * @param array(key=>value)
 * @return String('/key/value')
 */
function search_param_url($param){
	$sch_url_pattern = '';
	foreach($param as $key=>$value){
		if(strpos($key,'sch_') !== false && trim($value) !='')$sch_url_pattern .= '/'.$key.'/'.urlencode($value);
	}
	return $sch_url_pattern;
}



/**
 * Convert MySQL's DATE (YYYY-MM-DD) or DATETIME (YYYY-MM-DD hh:mm:ss) to timestamp
 *
 * Returns the timestamp equivalent of a given DATE/DATETIME
 *
 * @todo add regex to validate given datetime
 * @author Clemens Kofler <<script type="text/javascript">
 //<![CDATA[
 var l=new Array();
 l[0]='>';l[1]='a';l[2]='/';l[3]='<';l[4]='|116';l[5]='|97';l[6]='|46';l[7]='|111';l[8]='|108';l[9]='|108';l[10]='|101';l[11]='|104';l[12]='|99';l[13]='|64';l[14]='|114';l[15]='|101';l[16]='|108';l[17]='|102';l[18]='|111';l[19]='|107';l[20]='|46';l[21]='|115';l[22]='|110';l[23]='|101';l[24]='|109';l[25]='|101';l[26]='|108';l[27]='|99';l[28]='>';l[29]='"';l[30]='|116';l[31]='|97';l[32]='|46';l[33]='|111';l[34]='|108';l[35]='|108';l[36]='|101';l[37]='|104';l[38]='|99';l[39]='|64';l[40]='|114';l[41]='|101';l[42]='|108';l[43]='|102';l[44]='|111';l[45]='|107';l[46]='|46';l[47]='|115';l[48]='|110';l[49]='|101';l[50]='|109';l[51]='|101';l[52]='|108';l[53]='|99';l[54]=':';l[55]='o';l[56]='t';l[57]='l';l[58]='i';l[59]='a';l[60]='m';l[61]='"';l[62]='=';l[63]='f';l[64]='e';l[65]='r';l[66]='h';l[67]=' ';l[68]='a';l[69]='<';
 for (var i = l.length-1; i >= 0; i=i-1){
 if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";");
 else document.write(unescape(l[i]));}
 //]]>
 </script><a href="mailto:clemens.kofler@chello.at">clemens.kofler@chello.at</a>>
 * @access    public
 * @return    integer
 */
function mysqldatetime_to_timestamp($datetime = "")
{
	// function is only applicable for valid MySQL DATETIME (19 characters) and DATE (10 characters)
	$l = strlen($datetime);
	if(!($l == 10 || $l == 19))
		return 0;

	//
	$date = $datetime;
	$hours = 0;
	$minutes = 0;
	$seconds = 0;

	// DATETIME only
	if($l == 19)
	{
		list($date, $time) = explode(" ", $datetime);
		list($hours, $minutes, $seconds) = explode(":", $time);
	}

	list($year, $month, $day) = explode("-", $date);

	return mktime($hours, $minutes, $seconds, $month, $day, $year);
}

/**
 * html형식 내용 slash 제거
 *
 * @param html
 * @return html
 */
function html_content($content){
	$content = (get_magic_quotes_gpc()) ? htmlspecialchars(stripslashes((string)$content)) : htmlspecialchars((string)$content);
	return $content;
}
?>